<?php
	/*
	Plugin Name: Comment Karma
	Plugin URI: http://cyber-knowledge.net/blog/2006/10/15/wordpress-plugin-rate-your-comments-comment-karma/
	Description: Allows you to rate comments up or down.
	Author: Alex Bailey, modified by Jiwei Wang
	Author URI: http://cyber-knowledge.net
	Version: 2.o
   Chanages in v.2.0
      1. Thumb-up and thumb-down ratings are now separated, rather
      than a total sum.
      2. Reduce database query to one db dip per comment
      3. Improve installation procedue, optimize table with index
      4. Better error message in javascript response.
	*/ 

	/*
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	*/
	
	add_action('comment_post', 'ck_comment_posted'); //Hook into WordPress
	global $table_prefix, $wpdb;
		
	$table_name = $table_prefix . "ck_karma";
	if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
	{
		ck_install();
	}
	 
	function ck_install() //Install the needed SQl entries.
	{
		global $table_prefix, $wpdb;
		$table_name = $table_prefix . "ck_karma";

		$sql = 'DROP TABLE `' . $table_name . '`';  // drop the existing table
		mysql_query($sql);
		$sql = 'CREATE TABLE `' . $table_name . '` (' //Add table
			. ' `ck_comment_id` BIGINT(20) NOT NULL, '
			. ' `ck_ips` BLOB NOT NULL, '
			. ' `ck_rating_up` INT,'
			. ' `ck_rating_down` INT'
			. ' )'
			. ' ENGINE = myisam;';
		mysql_query($sql);
		$sql = 'ALTER TABLE `' . $table_name . '` ADD INDEX (`ck_comment_id`);';  // add index
		mysql_query($sql);

      echo "ck-karma tables created";
			 
		$ck_result = mysql_query('SELECT comment_ID FROM ' . $table_prefix . 'comments'); //Put all IDs in our new table
		while($ck_row = mysql_fetch_array($ck_result, MYSQL_ASSOC)) //Wee loop
		{
			mysql_query("INSERT INTO $table_name (ck_comment_id, ck_ips, ck_rating_up, ck_rating_down) VALUES ('" . $ck_row['comment_ID'] . "', '', 0, 0)");
		}
	}
	
	function ck_comment_posted($ck_comment_id) //When comment posted this executes
	{
		global $table_prefix, $wpdb;
		$table_name = $table_prefix . "ck_karma";
		mysql_query("INSERT INTO $table_name (ck_comment_id, ck_ips, ck_rating_up, ck_rating_down) VALUES ('" . $ck_comment_id . "', '" . getenv("REMOTE_ADDR") . "', 0, 0)"); //Adds the new comment ID into our made table, with the users IP
	}
	
	function ck_display_karma()
	{
		$plugin_path = get_bloginfo('url').'/wp-content/plugins/ck-karma/';
		$ck_link = str_replace('http://', '', get_bloginfo('url'));
		$ck_comment_ID = get_comment_ID();
		
		global $table_prefix, $wpdb;
		$table_name = $table_prefix . "ck_karma";
		$ck_sql = "SELECT ck_ips, ck_rating_up, ck_rating_down FROM `$table_name` WHERE ck_comment_id = $ck_comment_ID";
		$ck_result = mysql_query($ck_sql);
		
		if(!$ck_result)
		{
			mysql_error();
		}
      else if(!$ck_row = mysql_fetch_array($ck_result, MYSQL_ASSOC))
		{
			mysql_error();
		}
      else if(strstr($ck_row['ck_ips'], getenv("REMOTE_ADDR")))
		{
			echo("&nbsp;<img style='background: #000; padding: 0px; border: none;' src=\"{$plugin_path}/images/gray_up.png\" />");
		   echo "<small id=\"karma-{$ck_comment_ID}-up\"> {$ck_row['ck_rating_up']} </small>&nbsp;";
         echo("<img style='background: #000; padding: 0px; border: none;' src=\"{$plugin_path}/images/gray_down.png\" />"); 
         echo "<small id=\"karma-{$ck_comment_ID}-down\"> {$ck_row['ck_rating_down']} </small>&nbsp;";
		} else {
			//Use onClick for the image instead, fixes the style link underline problem as well.
			echo("&nbsp;Vote: <img style='background: #000; padding: 0px; border: none; cursor: pointer;' id=\"up-$ck_comment_ID\" src=\"{$plugin_path}images/up.png\" alt=\"Add rating\" onclick=\"javascript:karma('$ck_comment_ID', 'add', '{$ck_link}/wp-content/plugins/ck-karma/');\" />");
		   echo "<small id=\"karma-{$ck_comment_ID}-up\"> {$ck_row['ck_rating_up']} </small>&nbsp;";
			echo("<img style='background: #000; padding: 0px; border: none; cursor: pointer;\' id=\"down-$ck_comment_ID\" src=\"{$plugin_path}images/down.png\" alt=\"Subtract rating\" onclick=\"javascript:karma('$ck_comment_ID', 'subtract', '{$ck_link}/wp-content/plugins/ck-karma/')\" />"); //Phew
		   echo "<small id=\"karma-{$ck_comment_ID}-down\"> {$ck_row['ck_rating_down']} </small>&nbsp;";
		}
	}
	 
?>
